<%@ page import="utils.StringUtils" %>
<%@ page import="database.CourseDb" %><%--
  Created by IntelliJ IDEA.
  User: jimmyhsu
  Date: 2016/12/10
  Time: 15:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>导入课程中</title>
</head>
<body>
<%
    String courseName = StringUtils.getUtf8String(request.getParameter("courseName"));
    String creditStr = StringUtils.getUtf8String(request.getParameter("credit"));
    String yearStr = StringUtils.getUtf8String(request.getParameter("year"));
    String monthStr = StringUtils.getUtf8String(request.getParameter("month"));
    String dayStr = StringUtils.getUtf8String(request.getParameter("day"));
    String hourStr = StringUtils.getUtf8String(request.getParameter("hour"));
    String minuteStr = StringUtils.getUtf8String(request.getParameter("minute"));
    String location = StringUtils.getUtf8String(request.getParameter("location"));
    String durationStr = StringUtils.getUtf8String(request.getParameter("duration"));
    String maxNumberStr = StringUtils.getUtf8String(request.getParameter("maxNumber"));
    if (StringUtils.isEmpty(courseName) ||
            StringUtils.isEmpty(creditStr) ||
            StringUtils.isEmpty(yearStr) ||
            StringUtils.isEmpty(monthStr) ||
            StringUtils.isEmpty(dayStr) ||
            StringUtils.isEmpty(hourStr) ||
            StringUtils.isEmpty(minuteStr) ||
            StringUtils.isEmpty(location)) {
        response.getWriter().print("<script>alert('内容输入有误！请检查！');history.go(-1);</script>");
    } else {
        int credit = Integer.parseInt(creditStr);
        int duration = Integer.parseInt(durationStr);
        int maxNumber = Integer.parseInt(maxNumberStr);
        String datetime = yearStr + "-" + monthStr + "-" + dayStr + " " + hourStr + ":" + minuteStr + ":00";
        if (StringUtils.isEmpty(durationStr)) {
            duration = 120;
        }
        if (StringUtils.isEmpty(maxNumberStr)) {
            maxNumber = 999;
        }
        if (CourseDb.courseNotExists(courseName)) {
            CourseDb.insertCourse(courseName, credit, datetime, location, duration, maxNumber, 0);
            System.out.println("1");
            response.getWriter().print("<script>alert('导入成功！'); location.href='../importCourse.jsp';</script>");
        } else {
            System.out.println("2");
            response.getWriter().print("<script>alert('该课程名已存在，导入失败。'); location.href='../importCourse.jsp';</script>");
        }
    }
%>
</body>
</html>
